package com.gc.controller;


import com.gc.Common.result.Result;
import com.gc.Common.result.ResultUtils;
import com.gc.entity.OrderDetail;
import com.gc.entity.Orders;
import com.gc.entity.Type;
import com.gc.entity.User;
import com.gc.mapper.OrderMapper;
import com.gc.service.IOrderService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yuyingying
 * @since 2024-09-05
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    IOrderService service;
    @RequestMapping("/getList")
    public Result<Orders> getList(){
        List<Orders> list = service.list();
        return ResultUtils.returnSuccessLayui(list,list.size());
    }
    @RequestMapping("/del")
    public Object del(Integer id){
        if (service.removeById(id)){
            return ResultUtils.returnSuccess();
        }else{
            return ResultUtils.returnFail();
        }
    }

    @RequestMapping("/add")
    public Object add(Orders orders){
        orders.setOrderTime(new Date());
        if(service.save(orders)){
            return ResultUtils.returnDataSuccess(orders);
        }else{
            return ResultUtils.returnFail("添加失败");
        }
    }

    @RequestMapping("/update")
    public Object update(Orders orders) {
        if (service.updateById(orders)) {
            return ResultUtils.returnSuccess();
        } else {
            return ResultUtils.returnFail("修改失败");
        }
    }

    @Autowired(required = false)
    OrderMapper mapper;

    @RequestMapping("/getListAndDetail")
    public Result<Orders> getListAndDetail() {
        MPJLambdaWrapper wrapper = new MPJLambdaWrapper<Orders>()
                .selectAll(Orders.class) //查询所有：订单实体类
                .selectCollection(OrderDetail.class, Orders::getOrderDetails)
                .leftJoin(OrderDetail.class, OrderDetail::getOrderId, Orders::getId);
        wrapper.orderByDesc("id");
        List<Orders> list = mapper.selectJoinList(Orders.class, wrapper);
        return ResultUtils.returnDataSuccess(list);
    }




}
